import { Context } from 'koa';
const { getNotices, getNoticesByPageInfo, getByKeywordandPageInfo, getNoticesByKeyword, getInfoById, createNotice, update, deleteNot } = require('../service/notice.service')
const { invalidNoticeId, isEmpty } = require('../constants/err.type')

interface pageInfo {
    pageNum?: number,
    pageSize?: number
}

class NoticeController {
    // 获取公告列表接口
    public async getNotices(ctx: Context) {
        try {
            let res: any = null;
            let { pageNum, pageSize }: pageInfo = ctx.query;
            if (pageNum && pageSize) {
                pageNum = Math.max(pageNum * 1, 1);
                pageSize = Math.max(pageSize * 1, 1);
                res = await getNoticesByPageInfo(pageNum, pageSize);
            } else {
                res = await getNotices();
            }
            ctx.body = {
                code: 200,
                message: '获取公告列表成功',
                result: {
                    ...res
                }
            }
        } catch (error) {
            console.error('获取公告列表失败', error)
        }
    }
    // 根据关键词获取公告
    public async getNoticesByKeyword(ctx: Context) {
        try {
            let res: any = null;
            let { pageNum, pageSize }: pageInfo = ctx.query;
            const keyword  = ctx.query.keyword
            if(!keyword) return ctx.app.emit('error', isEmpty, ctx)
            if(pageNum && pageSize) {
                pageNum = Math.max(pageNum * 1, 1);
                pageSize = Math.max(pageSize * 1, 1);
                res = await getByKeywordandPageInfo(pageNum, pageSize, keyword);
            } else {
                res = await getNoticesByKeyword(keyword);
            }
            ctx.body = {
                code: 200,
                message: '搜索公告列表成功',
                result: {
                    ...res
                }
            }
        } catch (error) {
            console.error('根据关键词获取公告列表失败', error)
        }
    }
    // 根据id获取公告
    public async getNoticeById(ctx: Context) {
        try {
            const id = ctx.params.id
            const res: any = await getInfoById(id);
            if(res) {
                ctx.body = {
                    code: 200,
                    message: '根据id获取公告成功',
                    result: res
                }
            } else {
                return ctx.app.emit('error', invalidNoticeId, ctx)
            }
        } catch (error) {
            console.error('根据id获取公告失败', error)
        }
    }
    // 添加公告
    public async addNotice(ctx: Context) {
        try {
            const res = await createNotice(ctx.request.body)
            if(res) {
                ctx.body = {
                    code: 200,
                    message: `添加公告成功`
                }
            } else {
                ctx.body = {
                    code: 500,
                    message: `添加公告失败`
                }
            }
        } catch (error) {
            console.error('添加公告失败', error)
        }
    }
    // 更新公告
    public async updateNotice(ctx: Context) {
        try {
            const id = ctx.params.id
            const res = await update(id, ctx.request.body)
            if(res) {
                ctx.body = {
                    code: 200,
                    message: `更新公告成功`
                }
            } else {
                ctx.body = {
                    code: 500,
                    message: `更新公告失败`
                }
            }
        } catch (error) {
            console.error('更新公告失败', error)
        }
    }
    // 删除公告
    public async deleteNotice(ctx: Context) {
        try {
            const id = ctx.params.id
            const res = await deleteNot(id)
            if(res) {
                ctx.body = {
                    code: 200,
                    message: `删除公告成功`
                }
            } else {
                ctx.body = {
                    code: 500,
                    message: `删除公告失败`
                }
            }
        } catch (error) {
            console.error('更新公告失败', error)
        }
    }
}

module.exports = new NoticeController();